package cnn.test.controller;

import cnn.topfox.annotation.TokenOff;
import cnn.topfox.sql.Condition;
import cnn.user.entity.UserDTO;
import cnn.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/condition")
public class ConditionController {
    @Autowired
    UserService userService;

    /**
     * 条件匹配器的一个例子
     */
    @TokenOff
    @GetMapping("/query1")
    public List<UserDTO> query1(){
        //**查询 返回对象 */
        List<UserDTO> listUsers = userService.listObjects(
                Condition.create()
                    .between("age",10,20)
                    .eq("sex","男")          //生成 AND(sex = '男')
                    .eq("name","C","D","E")//生成 AND(name = 'C'  OR name = 'D' OR name = 'E')
                    .like("name","A", "B") //生成 AND(name LIKE '%A%' OR name LIKE '%B%')

                    //不等
                    .ne("name","张三","李四")

                    //自定义括号
                    .and("(")  //生成: and(
                    .eq("amount",10.10)
                    .or()//在此以后的所有字段都用 or
                    .eq("loginCount", 10)
                    .le("loginCount",11)
                    .add(")")// add 条件总能增加任意字符

                    .and()//因为前面写了 .or(),我们希望括号外面以后的的字段用and

                    .add("substring(name,2)='平' ")//自定义条件,要用到数据库的函数时可以这样写
                    .le("loginCount",1)//小于等于
                    .lt("loginCount",2)//小于
                    .ge("loginCount",4)//大于等于
                    .gt("loginCount",3)//大于

                    .isNull("name")
                    .isNotNull("name")
        );
        return listUsers;
    }

    @TokenOff
    @GetMapping("/query2")
    public List<UserDTO> query2(){
        //**查询 返回对象 */
        List<UserDTO> listUsers = userService.listObjects(
            userService.where()
                .eq("concat(name,id)","A1")          //生成 AND name = 'A'
                .eq("concat(name,id)","C1","D2","E3")//生成 AND(name = 'C'  OR name = 'D' OR name = 'E')
        );
        return listUsers;
    }
}